package com.lvmama.rhino.common.utils.JDBCUtil

import java.sql._

import com.mchange.v2.c3p0.ComboPooledDataSource

/**
  * Created by yuanxiaofeng on 2016/2/26.
  */
object ConnectionPool {
  @volatile var _pool: ConnectionPool = _
  def getMysqlManager: ConnectionPool = {
    if (_pool == null) {
      synchronized {
        if (_pool == null) {
          _pool = new ConnectionPool
        }
      }
    }
    _pool
  }
}

class ConnectionPool extends Serializable{
  private val cpds = new ComboPooledDataSource(true)

  val JDBCDefault = JDBCTemplate.JDBCDefaultSet
  val connP = JDBCTemplate.getConProperties
  try {
    cpds.setJdbcUrl(JDBCDefault.getOrElse("url", ""))
    cpds.setDriverClass("com.mysql.jdbc.Driver")
    cpds.setUser(JDBCDefault.getOrElse("user", ""))
    cpds.setPassword(JDBCDefault.getOrElse("password", ""))
    cpds.setMaxPoolSize(200)
    cpds.setMinPoolSize(20)
    cpds.setAcquireIncrement(5)
    cpds.setMaxStatements(180)
  } catch {
    case e: Exception => e.printStackTrace()
  }

  def getConnection: Connection = {
    try {
      cpds.getConnection()
    } catch {
      case ex: Exception =>
        ex.printStackTrace()
        null
    }
  }
}
